<?php
$objXajax->registerFunction('frmaccount_loadGrid');
$objXajax->registerFunction('frmaccount_edit');
$objXajax->registerFunction('frmaccount_save');
$objXajax->registerFunction('frmaccount_changePass');
function frmaccount_loadGrid()
{ 
    global $lang,$path;
    $objResponse =  new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = frmaccount_getGrid();  
    $oTbs =  new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/cus_account_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmaccount_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmaccount_tbl');");     
    $objResponse->addScript('changeWaiting(1);');                        
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}
function frmaccount_getGrid()
{
    global $objDbSelect,$hotel_id;
    $power_id = POWER_SUB_CUSTOMER;
    $sqlSelect = "  Select * from tbl_user where power_type_id=$power_id and hotel_id = $hotel_id";
          // echo    $sqlSelect; die();     
    return $objDbSelect->GetArray($sqlSelect);
} 

function frmaccount_edit($id){
        global $objDbSelect;
        $objResponse =  new xajaxResponse();
        $sql = " SELECT * FROM tbl_user WHERE id = '$id' ";
        $arr = $objDbSelect->GetArray($sql);
        
        $email        = addslashes($arr[0]['email']); 
        $phone     = addslashes($arr[0]['phone']);
        
        $address =htmlspecialchars_decode($arr[0]['address']) ;
        $username =htmlspecialchars_decode($arr[0]['username']) ;
        $objResponse->addAssign('frmaccount_txtAddress','value',$address);
        $objResponse->addAssign('frmaccount_txtUsername','value',$username);
        
        $objResponse->addScript("$('#frmaccount_txtEmail').val('$email');"); 
        $objResponse->addScript("$('#frmaccount_txtPhone').val('$phone');"); 
        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();   
    }
    
function frmaccount_save($params)
{
    global $objDbSelect,$user_id,$tbl_postfix;
    $objResponse =  new xajaxResponse();  
    $email =addslashes($params['txtEmail']) ;
    $address =addslashes($params['txtAddress']) ;
    $phone =addslashes($params['txtPhone']) ;
       
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $sql = ""; 
    if($edit!=1){
      $objResponse->addScript('frmaccount_alertSelect();');
      $objResponse->addScript('changeWaiting(1);');  
    }else{
       $sql ="UPDATE tbl_user SET email = '$email',address = '$address',phone = '$phone',
             user_update = '$user_id',date_update = now() WHERE id =$id"; 
/*        $objDbSelect->Execute($sql); 
        $objResponse->addScript('changeWaiting(1);');
        $objResponse->addScript('xajax_frmaccount_loadGrid();'); */
        if($objDbSelect->Execute($sql)){
       $objResponse->addScript("checkUpdateStatus(1,'frmaccount','$edit');");
       $objResponse->addScript('xajax_frmaccount_loadGrid();'); 
        }else{
       $objResponse->addScript("checkUpdateStatus(0,'frmaccount','$edit');");
    }
             
    }
    return $objResponse->getXML();
}

function frmaccount_changePass($newpass,$id)
{
    global $objDbSelect,$user_id,$tbl_postfix,$hotel_id;
    $objResponse =  new xajaxResponse();  
    if($_SESSION['current']['power_type_id']!=POWER_CUSTOMER){
        $objResponse->addAlert('Bạn không có quyền thực hiện chức năng này!');
    }else{
        $sql = " select hotel_id from tbl_user where id = $id ";
        $arr = $objDbSelect->GetArray($sql);
        if(is_array($arr)&& count($arr)>0){
           $service_id = $arr[0]['hotel_id']; 
           if($hotel_id!=$service_id){
             $objResponse->addAlert('Bạn chỉ có quyền thay đổi mật khẩu trên tài khoản con của bạn!');     
           }else{
             $pass= EncryptPass($newpass);
             $sql ="update tbl_user set pass = '$pass' where id = $id"; 
             $objDbSelect->Execute($sql);
           }
        }else{
         $objResponse->addAlert('Không tồn tại tài khoản cần đổi mật khẩu!');   
        }
    }
    return $objResponse->getXML();
} 
?>
